package com.neighbor.neighborhoodadmin.controller;

import com.neighbor.neighborhoodadmin.pojo.MallOrders;
import com.neighbor.neighborhoodadmin.pojo.MallProducts;
import com.neighbor.neighborhoodadmin.pojo.Notifications;
import com.neighbor.neighborhoodadmin.pojo.PageResult;
import com.neighbor.neighborhoodadmin.service.IMallOrdersService;
import com.neighbor.neighborhoodadmin.service.IMallProductsService;
import com.neighbor.neighborhoodadmin.service.INotificationsService;
import com.neighbor.neighborhoodadmin.utils.ResponseMsg;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商城兑换订单表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:11:23
 */
@RestController
@RequestMapping("/mallOrders")
public class MallOrdersController {
    @Autowired
    private IMallOrdersService mallOrdersService;
    @Autowired
    private IMallProductsService mallProductsService;
    @Autowired
    private INotificationsService notificationsService;
    //管理员获取所有订单列表
    @RequestMapping("/orders")
    ResponseMsg orders(@RequestParam(defaultValue = "0") Integer start,
                       @RequestParam(defaultValue = "5") Integer limit,
                       @RequestParam(defaultValue = "") String keyword,
                       @RequestParam(defaultValue = "") Integer status){
        List<MallOrders> ordersList = mallOrdersService.orders(start,limit, keyword, status);
        // 2. 获取符合条件的总条数
        Integer total = mallOrdersService.countOrders(keyword, status);
        System.out.println("total:"+total);
        // 3. 封装成包含data和total的对象
        PageResult<MallOrders> result = new PageResult<>(ordersList, total);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,result);
    }
    //管理员修改订单状态
    @RequestMapping("/updateStatus/{id}")
    ResponseMsg orders(@PathVariable Integer id,
                       @RequestParam Integer status
                       ){
        if(mallOrdersService.updateStatusById(id,status)==1){
            MallOrders order = mallOrdersService.getById(id);
            if(order.getUserId() != null){
                Integer userId = order.getUserId();
                StringBuilder title = new StringBuilder("订单");
                StringBuilder content = new StringBuilder("您购买的");
                MallProducts product = mallProductsService.getById(order.getProductId());
                String name = product.getName();
                switch (status){
                    case 1:
                        title.append("待确认");
                        content.append(name);
                        content.append("待确认");
                        break;
                    case 2:
                        title.append("进行中");
                        content.append(name);
                        content.append("进行中");
                        break;
                    case 3:
                        title.append("已完成");
                        content.append(name);
                        content.append("已完成");
                        break;
                    case 4:
                        title.append("已取消");
                        content.append(name);
                        content.append("已取消");
                        break;
                }
                Notifications notifications = new Notifications();
                notifications.setUserId(userId);
                notifications.setTitle(String.valueOf(title));
                notifications.setContent(String.valueOf(content));
                notifications.setType((byte) 2);
                notifications.setRelatedId(id);
                notifications.setIsRead((byte) 0);
                notifications.setCreatedAt(new Date());
                notificationsService.save(notifications);
            }
            return ResponseMsg.success();
        }

        return ResponseMsg.getInstance(ResponseMsg.PERMISSIONS_ERROR,"修改失败");
    }
}
